package com.hd.controller.activiti;

import com.hd.controller.base.BaseController;
import com.hd.entity.activiti.ElementDef;
import com.hd.entity.activiti.ElementIns;
import com.hd.service.activiti.ElementDefService;
import com.hd.service.activiti.ElementInsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 配置流程节点
 *
 * @author hzhh123
 * 2018年8月3日下午12:04:38
 */
@Controller
@RequestMapping("/element")
public class ElementController extends BaseController{
	@Autowired
	private ElementDefService elementDefService;
	@Autowired
	private ElementInsService elementInsService;
	/**
	 * 初始化流程节点
	 * @param procdefId 流程定义ID
	 * @return
	 */
	@RequestMapping("/initElementDef")
	@ResponseBody
	public Object initElementDef(String procdefId) {
		elementDefService.init(procdefId);
		return renderSuccess("初始化成功！"); 
	}
	
	/**
	 * 初始化流程节点页面
	 * @param procdefId
	 * @return
	 */
	@RequestMapping("/toElementIns")
	public String toElementIns(String procdefId,Model model) {
		model.addAttribute("procdefId", procdefId);
		List<ElementDef> elementDefs = elementDefService.find(procdefId);
		model.addAttribute("elementDefs", elementDefs);
		return "jsp/activiti/element/elementInit";
	}
	
	/**
	 * 查询节点轨迹
	 * @param procdefId
	 * @return
	 */
	@RequestMapping("/dataGrid/{procdefId}")
	public Object dataGrid(@PathVariable("procdefId")String procdefId) {
		return elementInsService.findByProcdefId(procdefId);
	}
	
	/**
	 * 跳转到添加节点轨迹
	 * @param procdefId
	 * @param model
	 * @return
	 */
	@RequestMapping("/toAddElementIns")
	public String toAddElementIns(String procdefId,Model model) {
		List<ElementDef> elementDefs = elementDefService.find(procdefId);
		model.addAttribute("elementDefs", elementDefs);
		return "jsp/activiti/element/elementInsAdd";
	}
	
	/**
	 * 添加节点轨迹
	 * @return
	 */
	@RequestMapping("/addElementIns")
	@ResponseBody
	public Object addElementIns(ElementIns elementIns) {
		elementInsService.save(elementIns);
		return renderSuccess("添加成功！");
	}
	/**
	 * 删除节点轨迹
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteElementIns/{id}")
	@ResponseBody
	public Object deleteElementIns(@PathVariable("id")String id) {
		elementInsService.delete(id);
		return renderSuccess("删除成功！");
	}

	/**
	 * 查看流程节点
	 * @param proceDefId
	 * @param model
	 * @return
	 */
	@RequestMapping("/toUpdateElementDef/{proceDefId}")
	public String toUpdateElementDef(@PathVariable("proceDefId") String proceDefId,Model model){
//		List<ElementDef>elementDefs=elementDefService.find(proceDefId);
//		model.addAttribute("elementDefs",elementDefs);
		return "jsp/activiti/deploy/updateElementdef";
	}

	/**
	 * 获取节点
	 * @param proceDefId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getElementDefsByProceDefId/{proceDefId}")
	public Object getElementDefsByProceDefId(@PathVariable("proceDefId")String proceDefId){
		List<ElementDef>elementDefs=elementDefService.find(proceDefId);
		return  elementDefs;
	}

	/**
	 * 更新节点顺序
	 * @param elementDef
	 * @return
	 */
	@RequestMapping("/UpdateElementDef")
	@ResponseBody
	public Object UpdateElementDef(ElementDef elementDef){
		logger.info("print={}",elementDef.isPrint());
		ElementDef vo=elementDefService.get(elementDef.getId());
		vo.setPrint(elementDef.isPrint());
		vo.setSeq(elementDef.getSeq());
		elementDefService.update(vo);
		return renderSuccess("更新成功！");
	}
}
